package com.google.coffeeshop.gui.desktop.seat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.table.DefaultTableModel;

import vn.bang.utility.DateUtil;

import com.google.coffeeshop.SeatStatus;
import com.google.coffeeshop.business.ManagerBusiness;
import com.google.coffeeshop.entity.SeatDTO;
import com.google.coffeeshop.gui.desktop.AbstractBaseTable;

public class SeatTable extends  AbstractBaseTable {

	private static final long serialVersionUID = 1L;
	
	private static String COL_SEAT_ID = "Product ID";
	private static String COL_CODE = "Code";
	private static String COL_STATUS = "Status";
	private static String COL_NAME = "Name";
	private static String COL_DESCRIPTION = "Description";
	private static String COL_CREATED_DATE = "Created Date";
	
	
	private static Object[][] rowData = new Object[][] {};
	private static String[] columnNames = new String[] {
		COL_SEAT_ID, COL_CODE, COL_STATUS, COL_NAME, COL_DESCRIPTION, COL_CREATED_DATE
		};
	
	private DefaultTableModel model = new DefaultTableModel(
			rowData,
			columnNames
		);
	
	public SeatTable() {
		super(rowData, columnNames);
		setModel(model);
		invisibleColumn(0);
	}
	
	private void invisibleColumn(int columnIndex) {
		this.getColumnModel().removeColumn(this.getColumnModel().getColumn(columnIndex));
	}
	
	public List<SeatDTO> getSeatList() {
		List<SeatDTO> employees = new ArrayList<SeatDTO>(); 
		for (int rowIndex = 0; rowIndex < model.getRowCount(); rowIndex++) {
			SeatDTO seat = getSeatSelected();
			employees.add(seat);
		}
		return employees;
	}
	
	public List<SeatDTO> getSelectedSeats() {
		List<SeatDTO> seats = new ArrayList<SeatDTO>();
		for (int rowIndex : getSelectedRows()) {
			SeatDTO seat = ManagerBusiness.findSeatById((long)findModelValue(model, COL_SEAT_ID, rowIndex));
			seats.add(seat);
		}
		return seats;
	}
	
	public long getSelectedProductId() {
		int rowIndex = this.getSelectedRow();
		return (long)findModelValue(model, COL_SEAT_ID, rowIndex);
	}
	
	public SeatDTO getSeatSelected() {
		int rowIndex = this.getSelectedRow();
		SeatDTO seat = new SeatDTO();
		seat.setId(
				(long)findModelValue(model, COL_SEAT_ID, rowIndex));
		seat.setCode(
				(String)findModelValue(model, COL_CODE, rowIndex));
		seat.setStatus(SeatStatus.valueOf(
				(String)findModelValue(model, COL_STATUS, rowIndex)).getStatusId());
		seat.setSeatName(
				(String)findModelValue(model, COL_NAME, rowIndex));
		seat.setDescription(
				(String)findModelValue(model, COL_DESCRIPTION, rowIndex));
		seat.setCreatedDate(
				DateUtil.toDate(
						(String)findModelValue(model, COL_CREATED_DATE, rowIndex)));
		return seat;
	}
	
	private void addNewRow(long seatId, String code, String status, String name, String description, Date createdDate) {
		Object[] objects = new Object[] {seatId, code, status, name, description, DateUtil.toString(createdDate)};
		model.addRow(objects);
	}

	private void addNewRow(SeatDTO seat) {
		addNewRow(seat.getId(),seat.getCode(), SeatStatus.getSeatStatus(seat.getStatus()).toString(), seat.getSeatName(), seat.getDescription(), seat.getCreatedDate());
	}
	
	public void addNewRows(List<SeatDTO> seats) {
		for (SeatDTO seat : seats) {
			addNewRow(seat);
		}
	}
}
